#include "Common.h"
#include "InputBoxGhost.h"
#include "PhysicsSystem.h"
#include "Engine.h"
#include <windows.h>

InputBoxGhost::InputBoxGhost()
	: mMoveVector(Vector3::Zero)
	, mRotateVector(Vector3::Zero)
	, mMoveSpeed(5.0f)
	, mRotateSpeed(50.0f)
{
}

InputBoxGhost::~InputBoxGhost()
{
}

bool InputBoxGhost::OnMouseMove(long deltaX, long deltaY)
{
	mRotation.X += Math::Trunc(-deltaX * mRotateSpeed);
	mRotation.Z += Math::Trunc(-deltaY * mRotateSpeed);
	return true;
}

bool InputBoxGhost::OnKeyDown(int key)
{
	switch (key)
	{
	case 'W':		mMoveVector.Z = 1;		break;
	case 'A':		mMoveVector.X = -1;		break;
	case 'S':		mMoveVector.Z = -1;		break;
	case 'D':		mMoveVector.X = 1;		break;
	case VK_UP:		mRotateVector.Z = 1;	break;
	case VK_DOWN:	mRotateVector.Z = -1;	break;
	case VK_LEFT:	mRotateVector.X = 1;	break;
	case VK_RIGHT:	mRotateVector.X = -1;	break;
	case VK_ADD:	mMoveSpeed += 1.0f;		break;
	case VK_SUBTRACT:mMoveSpeed -= 1.0f;	break;

	}
	return true;
}

bool InputBoxGhost::OnKeyUp(int key)
{
	switch (key)
	{
	case 'W':		mMoveVector.Z = 0;		break;
	case 'A':		mMoveVector.X = 0;		break;
	case 'S':		mMoveVector.Z = 0;		break;
	case 'D':		mMoveVector.X = 0;		break;
	case VK_UP:		mRotateVector.Z = 0;	break;
	case VK_DOWN:	mRotateVector.Z = 0;	break;
	case VK_LEFT:	mRotateVector.X = 0;	break;
	case VK_RIGHT:	mRotateVector.X = 0;	break;
	}
	return true;
}

bool InputBoxGhost::OnDeactive()
{
	mMoveVector = Vector3::Zero;
	mRotateVector = Vector3::Zero;
	return true;
}

void InputBoxGhost::Update(float elapsedTime)
{
	if (mMoveVector != Vector3::Zero)
	{
		mPosition += mMoveVector;
	}

	if (mRotateVector != Vector3::Zero)
	{
		Vector3 rotate(mRotateVector);
		rotate.Normalize();

		mRotateVector.X += Math::Trunc(rotate.X * mRotateSpeed);
		mRotateVector.Y += Math::Trunc(rotate.Y * mRotateSpeed);
		mRotateVector.Z += Math::Trunc(rotate.Z * mRotateSpeed);
	}
}

bool InputBoxGhost::MsgProc(void* hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_KEYDOWN:
		OnKeyDown(wParam);
		break;
	case WM_KEYUP:
		OnKeyUp(wParam);
		break;
	case WM_ACTIVATE:
		if (wParam == WA_INACTIVE)
			OnDeactive();
		break;
	default:
		return false;
	}
	return true;
}